home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / Cookie / Source / String.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.1 KB  |  133 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "String.h"
  5. #import <strings.h>
  6. #import <stdlib.h>
  7. #import <stdio.h>
  8. #import <NXCType.h>
  9. #import <c.h>
  10.  
  11. @implementation String
  12.  
  13. - setString:(char *)AString
  14. {
  15.     // If it's outta bounds theres nothing to do
  16.     if(!AString)
  17.         return self;
  18.  
  19.     // First see if we have to allocate space to the string
  20.     [self preAlloc :strlen(AString)+1];
  21.     strcpy(buffer,AString);            /* Copy in string */
  22.  
  23.     return self;
  24. }
  25.  
  26. - (char *)string
  27. {
  28.     return buffer;
  29. }
  30.  
  31. - freeString
  32. {
  33.     if(!buffer)
  34.         return self;    /* Get out if nothing to free */
  35.  
  36.     /* Free up string here */
  37.     free(buffer);
  38.     /* Blank buffer and length so we know it's blank */
  39.     buffer = 0;
  40.     length = 0;
  41.  
  42.     return self;
  43. }
  44.  
  45. - (BOOL)writeString :(NXStream *)MyStream
  46. {
  47.     if(length)
  48.         {
  49.             int strlength = strlen(buffer);
  50.  
  51.             if(NXWrite(MyStream,buffer,strlength) != strlength)
  52.                 return FALSE;    /* A Problem */
  53.         }
  54.  
  55.     return TRUE;
  56. }
  57.  
  58. - (BOOL)writeWithTerminator :(NXStream *)MyStream :(char)terminator
  59. {
  60.     return [self writeWithTerminator :MyStream :&terminator :1];
  61. }
  62.  
  63. - (BOOL)writeWithTerminator :(NXStream *)MyStream :(char *)terminator :(int)mylength
  64. {
  65.     if(![self writeString :MyStream])
  66.         return FALSE;
  67.  
  68.     if(NXWrite(MyStream,terminator,mylength) != mylength)
  69.         return FALSE;
  70.  
  71.     return TRUE;
  72. }
  73.  
  74. - (int)readString: (NXStream *)MyStream
  75. {
  76.     char TempBuffer[2024];
  77.     int mylength;
  78.  
  79.     mylength = 0;
  80.  
  81.     do
  82.         {
  83.         if(NXRead(MyStream,&TempBuffer[mylength],1) != 1)
  84.             break;    /* Problem!! */
  85.         if((!NXIsPrint(TempBuffer[mylength])) && (TempBuffer[mylength] != 0xA))
  86.             continue;
  87.         mylength++;
  88.         }
  89.     while(TempBuffer[mylength-1] != 0xA);
  90.  
  91.     if(mylength == 0) // Didn't read anything
  92.         return 0;
  93.  
  94.     TempBuffer[mylength-1] = 0; /* Strip off linefeed */
  95.  
  96.     /* Allocate some memory for our buffer */
  97.     [self setString:TempBuffer];
  98.  
  99.     return strlen(TempBuffer)+1;
  100. }
  101.  
  102. - (void)free
  103. {
  104.      [self freeString];
  105.      [super free];
  106. }
  107.  
  108. - init
  109. {
  110.      [super init];
  111.  
  112.      /* Do my initialisation */
  113.      buffer = 0;
  114.      length = 0;
  115.      
  116.      return self;
  117. }
  118.  
  119. - preAlloc :(int)size
  120. {
  121.     if(size <= length)
  122.         return self;    // we already have that size available
  123.  
  124.     [self freeString];
  125.     buffer = malloc(size);
  126.     buffer[0] = 0;
  127.     length = size;
  128.  
  129.     return self;
  130. }
  131.  
  132. @end
  133.